package com.luoluo.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.tools.view.VelocityLayoutServlet;

public class ViewVelocity extends VelocityLayoutServlet {

	private static final long serialVersionUID = -8257666970863694952L;

	private final static Log LOG = LogFactory.getLog(ViewVelocity.class);

	protected void error(HttpServletRequest req, HttpServletResponse res,
			Exception excp) throws ServletException {

		Throwable t = excp;
		if (excp instanceof MethodInvocationException)
			t = ((MethodInvocationException) excp).getWrappedThrowable();

		try {
			if (t instanceof ResourceNotFoundException) {
				// LOG.error(t.getMessage() + "(" +
				// req.getRequestURL().toString() + ")");
				if (!res.isCommitted())
					res.sendError(HttpServletResponse.SC_NOT_FOUND);
			} else {
				StringBuilder log = new StringBuilder(
						"ERROR Unknown Velocity Error url=");
				log.append(req.getRequestURL());
				if (req.getQueryString() != null) {
					log.append('?');
					log.append(req.getQueryString());
				}
				log.append('(');
				log.append(new Date());
				log.append(')');
				LOG.error(log.toString(), t);
				log = null;
				req.setAttribute("javax.servlet.jsp.jspException", t);
				res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			}
		} catch (IOException e) {
			LOG.error("Exception occured in VelocityServlet.error", e);
			throw new ServletException(e);
		} catch (IllegalStateException e) {
			LOG.error("==============<<IllegalStateException>>==============",
					e.getCause());
			throw new ServletException(e);
		}
		return;
	}

	private final static SimpleDateFormat format = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	protected void doRequest(HttpServletRequest req, HttpServletResponse res)
			throws IOException {

		super.doRequest(req, res);

		if (req.getAttribute("close_comment") == null) {
			PrintWriter pw = res.getWriter();
			pw.printf("\r\n<!-- Generated by LUOLUO :"
					+ format.format(new Date()) + "-->");
		}
	}
}
